// 用户登录 token个人信息的数据共享的模块, 后期自己写
import { reqLogin, reqGetUserInfo, reqGetUserDetailById } from '@/api/user'
import { setToken, getToken } from '@/utils/auth'
import { resetRouter } from '@/router'
export default {
  state: {
    token: getToken() || null, // 优先从cookie中获取 取不到为null
    userInfo: {} // 个人信息
  },
  mutations: {
    // 设置token的mutation
    setToken(state, token) {
      // 1. vuex存储token
      state.token = token
      // 2. cookie中存储token
      setToken(token)
    },
    // 设置个人信息的mutation
    setUserInfo(state, userInfo) {
      state.userInfo = userInfo
    }
  },
  actions: {
    // 登录的action
    async login(context, loginData) {
      // 1. 发送登录请求
      const res = await reqLogin(loginData)
      // console.log(res) // 登录的响应结果
      if (res.success) {
        // 2. 将token存储在vuex中
        context.commit('setToken', res.data)
      }
      return res // return 登录的响应结果, 要在页面中根据这个对象的状态提示
    },
    // 获取用户基本资料的action
    async getUserInfo(context) {
      const { data } = await reqGetUserInfo()
      const { data: data2 } = await reqGetUserDetailById(data.userId)
      // console.log(data)
      // console.log(data2)
      // console.log({ ...data, ...data2 })
      const fullData = { ...data, ...data2 }
      context.commit('setUserInfo', fullData)
      // console.log('获取资料成功了')
      return fullData // 将个人资料返回
    },
    // 退出的action
    logout(context) {
      context.commit('setToken', null)
      context.commit('setUserInfo', {})
      // 重置路由规则
      // 在user模块中提交permissions中的mutation, 参数三 {root: true} 根中查找
      context.commit('permissions/setRoutes', [], { root: true })
      // 重置路由
      resetRouter()
      // 初始化主题色
      context.commit('settings/resetTheme', null, { root: true })
    }
  },
  namespaced: true
}

/* import { login, logout, getInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: ''
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password } = userInfo
    return new Promise((resolve, reject) => {
      login({ username: username.trim(), password: password }).then(response => {
        const { data } = response
        commit('SET_TOKEN', data.token)
        setToken(data.token)
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { name, avatar } = data

        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
} */

